#!/bin/bash
# Copyright(c) 2020-2022, Intel Corporation
#
# Redistribution  and  use  in source  and  binary  forms,  with  or  without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of  source code  must retain the  above copyright notice,
#   this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# * Neither the name  of Intel Corporation  nor the names of its contributors
#   may be used to  endorse or promote  products derived  from this  software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
# IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT OWNER  OR CONTRIBUTORS BE
# LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
# CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT  OF
# SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
# INTERRUPTION)  HOWEVER CAUSED  AND ON ANY THEORY  OF LIABILITY,  WHETHER IN
# CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

source "${SCRIPT_DIR}/../version"

RPMBUILD_DIR="${SCRIPT_DIR}/rpmbuild"
SDK_DIR="$(cd "${SCRIPT_DIR}/../../../" && pwd)"


declare WHICH_RPM=''

if [ $# -lt 1 -o \( "$1" != 'rhel' -a "$1" != 'fedora' \) ]; then
  while [ "${WHICH_RPM}" != 'rhel' -a "${WHICH_RPM}" != 'fedora' ]; do
    read -p "Which RPM would you like to build? rhel or fedora> " WHICH_RPM
  done
else
  WHICH_RPM="$1"
fi
printf "Building %s RPM\n" "${WHICH_RPM}"

shopt -o -s nounset

declare -r RPM_SPEC="opae.spec.${WHICH_RPM}"

# Check for prerequisite packages.

declare -ra PYTHON_PKG_DEPS=(\
'python3-devel' \
'python36-devel' \
'python38-devel'\
)
declare -i HAVE_PYTHON_DEVEL=0

for pkg in ${PYTHON_PKG_DEPS[@]}
do
    dnf list installed "${pkg}" >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        HAVE_PYTHON_DEVEL=1
        break
    fi
done

if [ ${HAVE_PYTHON_DEVEL} -eq 0 ]; then
    printf "No suitable Python Development package found.. exiting\n"
    exit 1
fi

declare -a PKG_DEPS=(\
'make' \
'cmake' \
'git' \
'gcc' \
'gcc-c++' \
'json-c-devel' \
'libuuid-devel' \
'python3-jsonschema' \
'python3-pip' \
'rpm-build' \
'systemd' \
'rpmdevtools'\
)

if [ "${WHICH_RPM}" = 'fedora' ]; then
  PKG_DEPS=(${PKG_DEPS[@]} \
'python3-setuptools' \
'python3-pyyaml' \
'python3-pybind11' \
'tbb-devel' \
'hwloc-devel' \
'pybind11-devel' \
'cli11-devel' \
'spdlog-devel' \
'libedit-devel' \
'systemd-devel'\
)
fi

for pkg in ${PKG_DEPS[@]}
do
    dnf list installed "${pkg}" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        printf "${pkg} package not found.. exiting\n"
        exit 1
    fi
done

rm -rf "${RPMBUILD_DIR}"

CMAKE_BUILD_DIR="${SDK_DIR}/_build"
rm -rf ${CMAKE_BUILD_DIR}
mkdir ${CMAKE_BUILD_DIR}

cd ${CMAKE_BUILD_DIR}

if [ "${WHICH_RPM}" = 'rhel' ]; then
cmake ${SDK_DIR}  \
  -DOPAE_MINIMAL_BUILD=ON 
else
cmake ${SDK_DIR}   \
  -DOPAE_PRESERVE_REPOS=ON \
  -DOPAE_BUILD_PYTHON_DIST=ON \
  -DOPAE_BUILD_FPGABIST=ON 
fi

# Create source tarball.
cd "${SCRIPT_DIR}"

./create-tarball.sh opae-${PROJECT_VERSION}-${PROJECT_RELEASE}

# Create rpm build folder
mkdir -p "${RPMBUILD_DIR}"
for d in 'SOURCES' \
         'BUILD' \
         'RPMS' \
         'SPECS' \
         'BUILDROOT' \
         'SRPMS'
do
    mkdir -p "${RPMBUILD_DIR}/$d"
done

# Move tarball to where the rpms will be created
mv ${SDK_DIR}/../opae-${PROJECT_VERSION}-${PROJECT_RELEASE}.tar.gz .
# Copy the tarball to the rpm SOURCES dir
cp opae-${PROJECT_VERSION}-${PROJECT_RELEASE}.tar.gz "${RPMBUILD_DIR}/SOURCES"

cat "${SDK_DIR}/${RPM_SPEC}" | \
  sed -e "s/@VERSION@/${PROJECT_VERSION}/" \
      -e "s/@RELEASE@/${PROJECT_RELEASE}/" \
  > "${RPMBUILD_DIR}/SPECS/opae.spec"

# Create RPMS.
cd "${RPMBUILD_DIR}/SPECS"

# --noclean
rpmbuild --define="_topdir ${RPMBUILD_DIR}" -ba opae.spec
if [ $? -ne 0 ]; then
    printf "first rpmbuild failed.. exiting\n"
    exit 1
fi

# --noclean
rpmbuild --define="_topdir ${RPMBUILD_DIR}" -bs opae.spec
if [ $? -eq 1 ]; then
    echo "second rpmbuild failed.. exiting"
    exit 1
fi

# Move RPMS to script directory.
cd "${SCRIPT_DIR}"
mv "${RPMBUILD_DIR}"/RPMS/x86_64/opae-* "${SCRIPT_DIR}"
mv "${RPMBUILD_DIR}"/SRPMS/opae-*.src.rpm "${SCRIPT_DIR}"

# delete rpmbuild directory
rm -rf "$RPMBUILD_DIR"
rm -f opae.spec
